package com.tywj.buscustomerapp.view.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.daimajia.numberprogressbar.NumberProgressBar;
import com.tywj.buscustomerapp.R;
import com.tywj.buscustomerapp.model.bean.MonthTBean;
import com.tywj.buscustomerapp.model.bean.MyTicketBean;
import com.tywj.buscustomerapp.utils.ScreenUtils;
import com.tywj.buscustomerapp.utils.TimeRender;
import com.tywj.buscustomerapp.utils.WebServicrUtils;
import com.tywj.buscustomerapp.view.activity.MapTicketActivity;
import com.tywj.buscustomerapp.view.activity.MyMonthTicketActivity;
import com.tywj.buscustomerapp.view.activity.MyTicketActivity;
import com.tywj.buscustomerapp.view.myview.LoadingDialog;
import com.tywj.buscustomerapp.view.myview.ToastJoe;

import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Timer;
import java.util.TimerTask;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class ViewPagerTicketAdapter extends PagerAdapter {
    private Context mContext;
    private List<MonthTBean.DataBean> mList = new ArrayList<>();
    private Timer timer=null;
    private MyMonthTicketActivity activity;


    public ViewPagerTicketAdapter(Context mContext, List<MonthTBean.DataBean> mList, Activity activity) {
        this.mContext = mContext;
        this.mList = mList;
        this.activity = (MyMonthTicketActivity) activity;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    private Queue<View> viewPool = new LinkedList<>(); //View池

    @Override public Object instantiateItem(ViewGroup container, final int position) {
        View view;
        final ViewHolder viewHolder;
        //当池子中有存货就复用，否则才inflate
        if(viewPool.size() > 0){
            view = viewPool.poll();
            viewHolder = (ViewHolder) view.getTag();
        }else{
            view = LayoutInflater.from(mContext).inflate(R.layout.ticket_layout, container, false);
            viewHolder = new ViewHolder();
            viewHolder.textView = view.findViewById(R.id.ticket_date);
            viewHolder.imageView = view.findViewById(R.id.ticket_touch);
            viewHolder.name = view.findViewById(R.id.ticket_name);
            viewHolder.start = view.findViewById(R.id.start);
            viewHolder.startTIme = view.findViewById(R.id.start_time);
            viewHolder.end = view.findViewById(R.id.end);
            viewHolder.endTime = view.findViewById(R.id.end_time);
            viewHolder.number = view.findViewById(R.id.bus_number);
            viewHolder.checked = view.findViewById(R.id.checked);
            viewHolder.progressBar = view.findViewById(R.id.number_progress_bar);
            viewHolder.isMonthTicket = view.findViewById(R.id.ismonth);
            viewHolder.findBus = view.findViewById(R.id.bus_location);
            viewHolder.findBus.setVisibility(View.GONE);
            viewHolder.errorMessage = view.findViewById(R.id.error_info);
            view.setTag(viewHolder);

        }

        if (mList.get(position)!=null){
            viewHolder.isMonthTicket.setText("月票");
            viewHolder.textView.setText(mList.get(position).getYf()+"月");
            viewHolder.name.setText(mList.get(position).getXlmc());
            viewHolder.imageView.setPadding(0,0,0,0);
            Glide.with(viewHolder.imageView.getContext()).load(R.drawable.success).into(viewHolder.imageView);
            viewHolder.checked.setText("展示车票上车");
            viewHolder.startTIme.setText(mList.get(position).getScsj());
            viewHolder.endTime.setText(mList.get(position).getXcsj());
            viewHolder.start.setText(mList.get(position).getGmqsz());
            viewHolder.end.setText(mList.get(position).getGmzdz());
            viewHolder.number.setText(mList.get(position).getCph());
            viewHolder.findBus.setVisibility(View.VISIBLE);
            viewHolder.findBus.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(v.getContext(), MapTicketActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("month", mList.get(position));
                    intent.putExtras(bundle);
                    v.getContext().startActivity(intent);
                }
            });
        }

        container.addView(view);
        return view;
    }

    @Override public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
        //将当前View加入到池子中
        viewPool.offer((View) object);
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    //定义一个ViewHolder
    class ViewHolder{
        TextView  textView;
        ImageView imageView;
        TextView name;
        TextView startTIme;
        TextView endTime;
        TextView start;
        TextView end;
        TextView number;
        TextView checked;
        NumberProgressBar progressBar;
        TextView isMonthTicket;
        TextView findBus;
        TextView errorMessage;
    }

}
